<?php
/**
 * Created by PhpStorm.
 * User: jiayanxiu
 * Date: 15/11/30
 * Time: 17:38
 */
class SpecialApp extends MallbaseApp
{

    private $method;
    private $common;

    /**
     * SpecialApp constructor.
     */
    public function __construct() {
        $this->method = $_SERVER['REQUEST_METHOD'];
        $this->common = new CommonApp();
    }

    //获取专题分类
    /**
     * 专题分类
     * @author jyx
     * @date 2015/11/30
     */
    public function index() {
        $action = '获取专题分类';
        setLogParameter($action);
        if ($this->method != 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法的请求!');
        }
        $spec_id = $this->get_gp('spec_id', 'G');
        $page = $this->get_gp('page', 'G');
        $size = $this->get_gp('size', 'G');
        $spec_id = $spec_id ? intval($spec_id) : 0;
        $page = $page ? intval($page) : 1;
        $size = $size ? intval($size) : 10;
        $offset = ($page > 1) ? ($page - 1) * $size : 0;
        $user_id = $this->checkLogin();
        $model = m('goods');
        $sql = "SELECT id,cate_name FROM ".DB_PREFIX."special_category WHERE pid=0 AND sort>0 AND department = 1 ORDER BY sort DESC";
        $info = $model->getAll($sql);
        $condition = $spec_id == 0 ? "" : " AND cid={$spec_id}";
        //获取当前分类下的专题信息
        $sql = "SELECT id,`name`,content,start_time,end_time,ctype,video_url,share_text,share_img
                FROM ".DB_PREFIX."special WHERE `status`=1 {$condition} AND (start_time=0 or start_time<".time().") and (end_time=0 or end_time>".time().") AND department = 1 ORDER BY sort DESC LIMIT {$offset},{$size}";
        $list = $model->getAll($sql);
        //过滤没有商品的专题或者商品下架的专题
        if ($list) {
            $time = time();
            foreach ($list as $key=>$val) {
                //$type = $val['ctype'] ? 2 : 0;
                if($val['ctype']==0 || $val['ctype']==2){
                    $type=0;
                }
                $sql = "SELECT src FROM ".DB_PREFIX."special_img WHERE type={$type} AND spec_id=".$val['id'];
                $images = $model->getRow($sql);
                $val['img_path'] = $images['src'] ? QINIU_URL.$images['src'].SPECIAL_IMG : '';
                $val['url'] = getUrlByEnv("index.php?c=special&a=detail&id={$val['id']}&flag=1");
                $val['share_img'] = $val['share_img'] ? QINIU_URL.$val['share_img'] : '';
                $val['is_show'] = ($time > $val['start_time'] && $time < $val['end_time']) ? 1 : 0;//是否显示此专题
                //获取当前专题下的商品信息
                $sql = "SELECT g.goods_id,g.goods_name,g.introduction,g.default_image,g.price,g.source,g.currency,g.is_act,
                    g.discount_price,g.act_start_time,g.act_end_time,gs.collects,gs.base_collects FROM ".DB_PREFIX."goods g
                    LEFT JOIN ".DB_PREFIX."goods_statistics gs ON g.goods_id=gs.goods_id WHERE g.goods_id IN
                    (SELECT goods_id FROM ".DB_PREFIX."special_goods WHERE spec_id={$val['id']})
                    AND g.if_examine = 1 AND g.if_finance = 1 AND g.if_show = 1 AND g.closed = 0 GROUP BY g.goods_id
                    ORDER BY g.last_update DESC LIMIT 5;";
                $goods = $model->getAll($sql);
                if ($goods) {
                    foreach ($goods as $k=>$v) {
                        $v['collects'] = $v['collects'] + $v['base_collects'];
                        $v['discount'] = $this->common->isAct($v);
                        $v['follow'] = $user_id ? $this->common->getRelationShip($user_id, $v['goods_id']) : 0;//是否关注了此商品
                        $v['default_image'] = $this->common->isMySelfImage($v['default_image'], DISCOVERY_GOODS_IMG);
                        $goods[$k] = $v;
                    }
                }
                $val['goods'] = $goods;
                $list[$key] = $val;
            }
            //$info = array_merge(array(array("id"=>"0","cate_name"=>"All")), $info);
        }
        $data[0] = $info;
        $data[1] = $list;
        $this->ajax_return(FLAG_OK, '', $data);
    }


}